<?php
namespace Admin\Model;
use Think\Model;
class AdminUserModel extends Model
{
    /**
     * 用户登录认证
     * @param  string  $username 用户名
     * @param  string  $password 用户密码
     * @param  integer $type     用户名类型 （1-用户名，2-邮箱，3-手机，4-UID）
     * @return integer           登录成功-用户ID，登录失败-错误编号
     */
    public function login($username, $password, $type = 1)
    {
        switch ($type)
        {
            case 1 :
                $data  =array(
                    'username'  =>$username,
                    'password'  =>$password,
                    'status'    =>'1'
                );
                break;
            case 2:
                $data  =array(
                    'email'     =>$username,
                    'password'  =>$password,
                    'status'    =>'1'
                );
                break;
            case 3:
                $data  =array(
                    'phone'     =>$username,
                    'password'  =>$password,
                    'status'    =>'1'
                );
                break;
        }
        $user = $this->field(true)->where($data)->find();
        
        if(!$user || 1 != $user['status']) {
            $this->error = '用户不存在或已被禁用！'; //应用级别禁用
            return false;
        } else {
            $uid = $user['uid'];
            //记录行为
            action_log('user_login', 'member', $uid, $uid);
            //存入session
            session('uid', $uid);
            /* 登录用户 */
            $this->autoLogin($user);
            return true;
        }
    }
    
}